<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear='getCateList'>
            <el-button slot="append" icon="el-icon-search" @click="searchCateList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addCategoryDialogOpen">添加商品分类</el-button>
        </el-col>
      </el-row>
      <zk-table
        class="table"
        ref="table"
        border
        show-index
        :data="cateList"
        index-text='序号'
        :columns="columns"
        :show-row-hover='false'
        :selection-type = 'false'
        :expand-type = 'false'
        >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:lightgreen;" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" style="color:red;" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1" type="success">二级</el-tag>
          <el-tag v-else type="error">三级</el-tag>
        </template>
        <template slot="option" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click= "editCategoryOpen(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click= "delCategory(scope.row)">删除</el-button>
        </template>
      </zk-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <div>
        sas
        </div>
      <!-- 添加分类模态 -->
      <el-dialog
        title="添加商品分类"
        :visible.sync="addCategoryDialogVisible"
        width="50%"
        @close="addCategoryDialogClose">
        <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addForm.cat_name" autocomplete="off" ></el-input>
          </el-form-item>
          <el-form-item label="父级分类" prop="cat_pid">
            <el-cascader
              v-model="selectKeys"
              :options="parentCatList"
              :props="propsConfig"
              clearable
              @change="parentCatListChange">
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCategoryDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveAddCategory">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑模态 -->
      <el-dialog
        title="编辑商品分类"
        :visible.sync="editCategoryDialogVisible"
        width="50%"
        @close="editCategoryDialogClose">
        <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px" >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="editForm.cat_name" autocomplete="off" ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editCategoryDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveEditCategory">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { categoryList, addCategory, deleteCategory, editCategory } from '@/api/categories'
import { catNameValidate } from '@/validate/validate'
export default {
  name: 'Categories',
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      addCategoryDialogVisible: false,
      editCategoryDialogVisible: false,
      cateList: [],
      parentCatList: [],
      selectKeys: [],
      total: 0,
      columns: [
        {
          label: '名称',
          prop: 'cat_name',
          width: '200px'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          width: '100px',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          width: '200px',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'option',
          width: '200px'
        }
      ],
      addForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      editForm: {
        cat_name: '',
        cat_id: null
      },
      rules: {
        cat_name: [
          { required: true, validator: catNameValidate, trigger: 'blur' }
        ]
      },
      propsConfig: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      }
    }
  },
  created () {
    this.getCateList()
  },
  mounted () {
    setInterval(function () {
      document.querySelectorAll('.el-cascader-node__label').forEach(el => {
        el.onclick = function () {
          if (this.previousElementSibling) this.previousElementSibling.click()
        }
      })
    }, 1000)
  },
  methods: {
    // 搜索
    searchCateList () {

    },
    async getCateList () {
      const { data: res } = await categoryList(this.queryInfo)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getCateList()
    },
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getCateList()
    },
    addCategoryDialogOpen () {
      this.getCatParentList()
      this.addCategoryDialogVisible = true
    },
    // 获取父级分类列表
    async getCatParentList () {
      const params = {
        type: 2
      }
      const { data: res } = await categoryList(params)
      this.parentCatList = res.data
    },
    // 父级选择改变
    parentCatListChange (val) {
      // 有父级分类
      if (this.selectKeys.length > 0) {
        // 父级分类id
        this.addForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addForm.cat_level = this.selectKeys.length
      } else {
        this.addForm.cat_pid = 0
        this.addForm.cat_level = 0
      }
    },
    saveAddCategory () {
      this.$refs.addForm.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await addCategory(this.addForm)
        if (res.meta.status === 201) {
          this.$message.success(res.meta.msg)
          this.addCategoryDialogClose()
          this.getCateList()
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    },
    addCategoryDialogClose () {
      this.addForm = {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      }
      this.selectKeys = []
      this.addCategoryDialogVisible = false
    },
    editCategoryOpen (row) {
      this.editCategoryDialogVisible = true
      this.editForm.cat_name = row.cat_name
      this.editForm.cat_id = row.cat_id
    },
    saveEditCategory () {
      this.$refs.editForm.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await editCategory(this.editForm)
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
          this.editCategoryDialogClose()
          this.getCateList()
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    },
    editCategoryDialogClose () {
      this.editForm = {
        cat_name: '',
        cat_id: null
      }
      this.editCategoryDialogVisible = false
    },
    delCategory (row) {
      console.log(row.cat_id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await deleteCategory(row.cat_id)
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
          this.getCateList()
        } else {
          this.$message.error(res.meta.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>
<style lang='less' scoped>
  .table{
    margin-top: 20px;
  }
  .el-cascader{
    width: 100%;
  }
  ::v-deep .el-radio {
    // color: #606266;
    color: red;
    cursor: pointer;
    visibility: hidden; // 加上这一行
    margin-right: 30px}

</style>
